home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-02-09 | 3.1 KB | 118 lines | [TEXT/PJMM] |
- unit FileUtils;
-
- { Created June 11, 1989, to aid Toolbox file calls. }
-
- interface
-
- uses
- Globals;
-
- const
- BUFFSIZE = 32768;
-
- type
- FileBuffer = packed array[1..BUFFSIZE] of char;
- BuffPtr = ^FileBuffer;
- BuffHndl = ^BuffPtr;
-
- var
- InFileLength, InBuffIndex, InputAmount, ImportPos, BufferFilePos: longint;
- InBuffHndl: BuffHndl;
- Leftover: STR255;
-
- function TReadALine (FileRefNum: integer): STR255;
-
- procedure FillBuffer (FileRefNum: integer);
-
- implementation
-
- { ------------------------------------------------------ }
-
- procedure FillBuffer;
-
- { Fills file buffer with BUFFSIZE characters, or to end of file }
- { Remember to initialize InBuffHndl before calling! }
-
- var
- CharCount: longint;
- FileError: OSErr;
-
- begin
- FileError := SetFPos(FileRefNum, fsFromStart, BufferFilePos);
- if InFileLength < (ImportPos + BUFFSIZE) then
- InputAmount := InFileLength - ImportPos
- else
- InputAmount := BUFFSIZE;
- FileError := FSRead(FileRefNum, InputAmount, Ptr(InBuffHndl^));
- InBuffIndex := 1;
- FileError := GetFPos(FileRefNum, BufferFilePos);
- end;
-
- { ------------------------------------------------------ }
-
- procedure BreakMsgLine (var MsgTxtString: str255);
-
- { On entry MsgTxtString is MAXMSGLINE or more in length. Looks for natural break and splits }
- { line into MsgTxtString and Leftover components. If no break found, MsgTxt string is }
- { MAXMSGLINE long and Leftover is empty. }
-
- var
- i: integer;
-
- begin
- for i := length(MsgTxtString) downto 1 do
- if MsgTxtString[i] in [SPACE, TAB, ENDLINE] then
- begin
- Leftover := copy(MsgTxtString, i + 1, 255);
- MsgTxtString := copy(MsgTxtString, 1, (i - 1));
- leave;
- end;
- if (length(MsgTxtString) > MaxMsgLine) then
- begin
- Leftover := concat(Leftover, copy(MsgTxtString, succ(MaxMsgLine), 255));
- MsgTxtString := copy(MsgTxtString, 1, MaxMsgLine);
- end;
- while (Leftover[1] in [SPACE, TAB, ENDLINE]) & (length(Leftover) > 0) do
- Leftover := copy(Leftover, 2, 255)
- end;
-
- { ------------------------------------------------------ }
-
- function TReadALine;
-
- { Reads a line of characters from a text file, returns string }
- { Remember to initialize InBuffHndl before calling! }
- { Modified 2/91 to break long lines at MaxMsgLine characters. }
-
- var
- LineString: STR255;
-
- begin
- LineString := Leftover;
- Leftover := '';
- while (InBuffHndl^^[InBuffIndex] <> ENDLINE) & (ImportPos < InFileLength) & (length(LineString) < MaxMsgLine) do
- begin
- LineString := concat(LineString, InBuffHndl^^[InBuffIndex]);
- InBuffIndex := succ(InBuffIndex);
- ImportPos := succ(ImportPos);
- if InBuffIndex > BUFFSIZE then
- FillBuffer(FileRefNum)
- end;
-
- if ((InBuffHndl^^[InBuffIndex] <> ENDLINE) & (ImportPos < InFileLength)) | (length(LineString) > MaxMsgLine) then {must be long}
- BreakMsgLine(LineString);
-
- if (InBuffHndl^^[InBuffIndex] = ENDLINE) & (ImportPos < InFileLength) then
- begin
- InBuffIndex := succ(InBuffIndex);
- ImportPos := succ(ImportPos);
- if InBuffIndex > BUFFSIZE then
- FillBuffer(FileRefNum)
- end;
-
- TReadALine := LineString
- end;
-
- { ------------------------------------------------------ }
-
- end. { Unit }